﻿using GameFramework.Event;
using Sirenix.OdinInspector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using TowerDefence_Zombie.Data;
using UnityEngine;
using UnityEngine.UI;

namespace TowerDefence_Zombie
{
    public class UIDoubleAtkSkillFrom : UGuiFormEx
    {
        [Title("显示时间"), SerializeField] public TextMeshProUGUI timeText;

        [Title("购买按钮"), SerializeField] public Button buyBtn;

        [Title("关闭按钮"), SerializeField] public Button closeBtn;

        [Title("够买进度"), SerializeField] public Image buyProgress;


        protected override void OnInit(object userData)
        {
            base.OnInit(userData);
        }

        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);
            GameEntry.Event.Subscribe(UpdateDoubleAtkSkillTimeEventArgs.EventId, SetTimeText);
            buyBtn.interactable = GameEntry.Data.GetData<DataPlayer>().CheckCurrencyIsMeet(102, 20);
            buyProgress.fillAmount = (float)GameEntry.Data.GetData<DataPlayer>().GetPlayerData().PlayerSkillData.DoubleAttackCount / 5;
            buyBtn.onClick.AddListener(BuyDoubleAtkSkill);
            closeBtn.onClick.AddListener(CloseForm);
        }

        protected override void OnClose(bool isShutdown, object userData)
        {
            base.OnClose(isShutdown, userData);
            GameEntry.Event.Unsubscribe(UpdateDoubleAtkSkillTimeEventArgs.EventId, SetTimeText);
            buyBtn.onClick.RemoveListener(BuyDoubleAtkSkill);
        }

        private void BuyDoubleAtkSkill()
        {
            GameEntry.Data.GetData<DataPlayer>().RemoveCurrency(102, 20, CurrencyEvent.BuyDoubleAtkBuff);
            var atkTime = GameEntry.Data.GetData<DataPlayer>().GetPlayerData().PlayerSkillData.RemainDoubleAttackTime + 3600;
            if (atkTime > 18000)
                atkTime = 18000;
            GameEntry.Data.GetData<DataPlayer>().GetPlayerData().PlayerSkillData.RemainDoubleAttackTime = atkTime;
            GameEntry.Data.GetData<DataPlayer>().GetPlayerData().PlayerSkillData.DoubleAttackCount++;
            if (GameEntry.Data.GetData<DataPlayer>().GetPlayerData().PlayerSkillData.DoubleAttackCount >= 5)
            {
                GameEntry.Data.GetData<DataPlayer>().AddCurrency(102, 50, CurrencyEvent.DoubleAtkBuffReturn);
                GameEntry.Data.GetData<DataPlayer>().GetPlayerData().PlayerSkillData.DoubleAttackCount = 0;
            }
            buyProgress.fillAmount = (float)GameEntry.Data.GetData<DataPlayer>().GetPlayerData().PlayerSkillData.DoubleAttackCount / 5;
            buyBtn.interactable = GameEntry.Data.GetData<DataPlayer>().CheckCurrencyIsMeet(102, 20);

            GameEntry.Event.Fire(this, TriggerDoubleAtkEventArgs.Create());
        }


        private void SetTimeText(object sender, GameEventArgs e)
        {
            UpdateDoubleAtkSkillTimeEventArgs ne = (UpdateDoubleAtkSkillTimeEventArgs)e;
            timeText.text = ne.RemainingTimeStr;
        }


        void CloseForm()
        {
            base.Close();
        }

    }
}
